package com.yanqu.road.server.cmd.activity.commonteam;

import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeam;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C_CROSS_AUTO_SEND_COMMON_TEAM_RANK_LIST, desc = "通用组队-补发队伍排行奖励，获取队伍排行")
public class CrossCommonTeamAutoSendTeamRankCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        AutoSendRewardProto.NoticeCrossUserRankMsg reqMsg =  AutoSendRewardProto.NoticeCrossUserRankMsg.parseFrom(packet.getBytes());
        //获取活动
        CommonTeamActivity activity = CommonTeamMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            CommonTeamMgr.getLogger().info("通用组队-补发队伍排行奖励：活动不存在");
            return;
        }
        //构造榜单数据，返回游戏服
        AutoSendRewardProto.CrossUserRankMsg.Builder builder = AutoSendRewardProto.CrossUserRankMsg.newBuilder();
        builder.setActivityId(reqMsg.getActivityId());
        //获取榜单类型
        List<Integer> conditionTypes = CommonTeamMgr.getTeamRankConditionTypes(reqMsg.getActivityId());
        if (conditionTypes != null && !conditionTypes.isEmpty()) {
            //遍历榜单类型，获取对应榜单（一种活动可能有多个队伍榜单）
            for (int conditionType : conditionTypes) {
                List<CommonTeam> teamRankList = activity.getTeamRankList(conditionType);
                int rank = 1;
                //遍历队伍榜单
                for (CommonTeam commonTeam : teamRankList) {
                    //遍历队伍成员
                    for (CommonTeamUserData commonTeamUserData : commonTeam.getMemberDataMap().values()) {
                        AutoSendRewardProto.UserRankMsg.Builder userRankMsg = AutoSendRewardProto.UserRankMsg.newBuilder();
                        userRankMsg.setUserId(commonTeamUserData.getUserId());
                        userRankMsg.setRank(rank);
                        userRankMsg.setConditionType(conditionType);
                        builder.addData(userRankMsg);
                    }
                    rank++;
                }
            }
            YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_ACTIVITY_AUTO_SEND_USER_REWARD, builder);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
        }
    }

    @Override
    public void sendData() {

    }
}
